/***
 * Excerpted from "Stripes: and Java Web Development is Fun Again",
 * published by The Pragmatic Bookshelf.
 * Copyrights apply to this code. It may not be used to create training material, 
 * courses, books, articles, and the like. Contact us if you are in doubt.
 * We make no guarantees that this code is fit for any purpose. 
 * Visit http://www.pragmaticprogrammer.com/titles/fdstr for more book information.
***/
package stripesbook.dao.impl.stripersist;

import org.stripesstuff.stripersist.Stripersist;
import java.util.List;
import stripesbook.dao.ContactDao;
import stripesbook.model.Contact;
import stripesbook.model.User;

public class ContactDaoImpl extends BaseDaoImpl<Contact,Integer>
    implements ContactDao
{
    public Contact findByEmail(String email, User user) {
        return findBy("email", email, user);
    }
    
    @SuppressWarnings("unchecked")
    public List<Contact> findByName(String startsWith, User user) {
        return Stripersist.getEntityManager()
            .createQuery("select distinct c from "
              + getEntityClass().getName() + " c "
              + "where (c.firstName like '" + startsWith + "%' or "
              + "c.lastName like '" + startsWith + "%') "
              + "and c.user = :user"
             ).setParameter("user", user).getResultList();
    }
    
}
